<?php
/**
 * Create a custum template based
 * error page.
 *
 * @author Mark Dohmen.
 */
final class ClientException extends Exception {
	
	/**
	 * Create the error.
	 *
	 * @return $this; for chaining.
	 */
	final public function createError() {
		Client::getClient()->getSmarty()->assign("code", $this->getCode());
		Client::getClient()->getSmarty()->assign("line", $this->getLine());
		Client::getClient()->getSmarty()->assign("message", $this->getMessage());
		Client::getClient()->getSmarty()->assign("file", $this->getFile());
		return $this;
	}
	
	/**
	 * Now lets see the actual error page :).
	 */
	final public function showError() {
		echo Client::getClient()->getSmarty()->display($this->getErrorTemplate());
	}
	
	/**
	 * Gets the error page template file.
	 * Note:
	 *
	 *    This function will search for a few folders
	 *    named 'system' & 'error' in your selected, 
	 *    templates folder and check if a file,
	 *    named:
	 *    
	 *      error.page.tpl
	 *
	 *    When neither the folter or the file is found
	 *    then we'll use the default one.
	 *
	 * @return 
	 */
	final public function getErrorTemplate() {
		$errorTemplate = "./client/templates/default/system/error/error.page.tpl"; //TODO: change variabel template name.
		if (!file_exists($errorTemplate)) {
			$errorTemplate = "./client/templates/default/system/error/error.page.tpl";
		}
		if (!file_exists($errorTemplate)) {
			die(
				"Well this is sad :(, it seems we even have trouble loading the error page..." . 
				"<br /><br />Original error:<br />Error code: " . 
				$this->getCode() . "<br />Error line: " . 
				$this->getLine() . "<br />Error message: " . 
				$this->getMessage() . "<br />Error file: " . 
				$this->getFile()
			);
		}
		return (string) $errorTemplate;
	}
}
?>